/*
 * Copyright (c) 2005, 2020, Jin Lin All rights reserved.
 * Jin Lin PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package aco.module.iteration;


import aco.entity.dobject.Cost;
import aco.entity.dobject.Node;
import aco.entity.dobject.Task;
import aco.entity.dobject.Trainer;
import aco.entity.dtobject.BaseDataDTO;

import java.util.List;

/**
 * 描述 迭代抽象.
 *
 * @author Jin Lin
 * @created 2019 -06-30 14:47:32
 */
public abstract class AbstractSearchHandle {

    /**
     * 描述 初始化+算法迭代.
     *
     * @author Jin Lin
     * @created 2019 -06-30 14:46:07 Do iteration.
     */
    public void doIteration() {
        BaseDataDTO baseDataDTO = initBaseData();
        Trainer trainer = initTaskInfoAndPheromone(baseDataDTO.getCost());
        search(baseDataDTO, trainer);
    }

    /**
     * 描述 任务派发函数.
     *
     * @param antCount      the ant count
     * @param criticalPoint the critical point
     * @param nodes         the nodes
     * @param task          the task
     * @return the long
     * @author Jin Lin
     * @created 2019 -07-09 22:36:19 Assign task long.
     */
    public Integer assignTask(Long antCount, Long criticalPoint, List<Node> nodes, Task task) {
        // 若当前蚂蚁编号在临界点之前，则采用最大信息素的分配方式
        if (antCount <= criticalPoint) {
            return task.getOptimal();
        }
        // 若当前蚂蚁编号在临界点之后，则采用随机分配方式
        return new Double(Math.random() * 15).intValue();
    }

    /**
     * 描述 初始化基本数据.
     *
     * @return the base data dto
     * @author Jin Lin
     * @created 2019 -06-30 14:46:07 Init base data.
     */
    abstract BaseDataDTO initBaseData();

    /**
     * 描述 初始化算法参数.
     *
     * @param cost the cost
     * @return the ant
     * @author Jin Lin
     * @created 2019 -06-30 14:46:07 Init task info and pheromone.
     */
    abstract Trainer initTaskInfoAndPheromone(Cost cost);


    /**
     * 描述 搜索迭代.
     *
     * @param baseDataDTO the base data dto
     * @param trainer     the trainer
     * @author Jin Lin
     * @created 2019 -07-01 22:11:15 Search.
     */
    abstract void search(BaseDataDTO baseDataDTO, Trainer trainer);

}
